// pages/menu/menu.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeKey: 0,
    selIndex: 0, //默认被选中第一个的下标

    MenuLoading: true,
    navShow: true,
    navMenuShow: true,

    menuData: [],
    navData: [],
    navMenuData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this

    this.getMenuData(that)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  getMenuData(that) {
    wx.request({
      url: 'https://apis.netstart.cn/douguo/recipe/flatcatalogs',
      method: 'GET',

      success: function (res) {
        that.setData({
          menuData: res.data.result.cs,
          navData: res.data.result.cs[0].cs,
        })

        const keyword = that.data.navData[0].name

        that.getNavMenuData(that, keyword)
      },

      complete() {
        that.setData({
          MenuLoading: false,
          navShow: false
        })
      }

    })
  },

  getNavMenuData(that, keyword) {
    wx.request({
      url: 'https://apis.netstart.cn/douguo/recipe/search',
      method: "GET",
      data: {
        "keyword": keyword
      },

      success: function (res) {
        const list = res.data.result.list
        const newList = []

        for (const item in list) {
          if (list[item].type == 13) {
            newList.push(list[item].r)
          }
        }

        that.setData({
          navMenuData: newList
        })
      },

      complete() {
        that.setData({
          navMenuShow: false
        })
      }
    })
  },

  onChange(event) {
    var that = this
    const index = event.detail

    that.setData({
      navData: that.data.menuData[index].cs
    })

    const keyword = that.data.menuData[index].cs[0].name

    that.setData({
      selIndex: 0,
      navMenuShow: true
    })

    that.getNavMenuData(that, keyword)
  },

  onChangeNav(event) {
    var that = this
    const index = event.currentTarget.dataset.index
    const keyword = that.data.navData[index].name

    that.setData({
      navMenuShow: true
    })

    that.getNavMenuData(that, keyword)

    that.setData({
      selIndex: index
    })
  },

  onClickJump(event) {
    const id = event.currentTarget.dataset.id

    wx.navigateTo({
      url: '../note/note?id=' + id
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})